#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>


#define END(condition,func) if(condition) { \
    qDebug() << #func << " error";  \
    goto end; \
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{

   const char* bmp_path = "/Users/tgy/Documents/ffmpeg/image/demo.bmp";
   SDL_Surface* surface = nullptr;
   SDL_Window* window = nullptr;
   SDL_Renderer* renderer = nullptr;
   SDL_Texture* texture = nullptr;
   SDL_Event event;
   SDL_Rect src_rect = {250,250,20,20};
   SDL_Rect dst_rect   = {0,0,20,20};
   SDL_Texture* customTexture = nullptr;
   SDL_Rect custom_rect = {100,100,50,50};

   END(SDL_Init(SDL_INIT_VIDEO),SDL_Init)

   surface  = SDL_LoadBMP(bmp_path);

   END(!surface,SDL_LoadBMP);


   window = SDL_CreateWindow("显示BMP图片",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,surface->w,surface->h,SDL_WINDOW_SHOWN);

   END(!window,SDL_CreateWindow);

   renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC);

   if(!renderer) {

       renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_SOFTWARE);
   }

   END(!renderer,SDL_CreateRenderer);

   customTexture = createTexture(renderer);
   SDL_SetRenderTarget(renderer,nullptr);

   texture = SDL_CreateTextureFromSurface(renderer,surface);


   END(!texture,SDL_CreateTextureFromSurface);


   END(SDL_SetRenderDrawColor(renderer,255,0,0,SDL_ALPHA_OPAQUE),SDL_SetRenderDrawColor);

   END(SDL_RenderClear(renderer),SDL_RenderClear);

   SDL_SetRenderDrawColor(renderer,255,255,255,SDL_ALPHA_OPAQUE);

   SDL_RenderDrawLine(renderer,50,50,100,100);
   SDL_RenderDrawLine(renderer,50,100,100,50);

   END(SDL_RenderCopy(renderer,texture,&src_rect,&dst_rect),SDL_RenderCopy);

   SDL_RenderCopy(renderer,customTexture,nullptr,&custom_rect);

   SDL_RenderPresent(renderer);


   while (true) {

       SDL_WaitEvent(&event);

       if(event.type == SDL_QUIT) {


           break;
       }else if(event.type == SDL_MOUSEBUTTONDOWN) {

           mouseClick(renderer,customTexture,event);
       }

   }

end:

   if(customTexture) {

       SDL_DestroyTexture(customTexture);
   }

   SDL_FreeSurface(surface);
   SDL_DestroyTexture(texture);
   SDL_DestroyRenderer(renderer);
   SDL_DestroyWindow(window);
   SDL_Quit();

}

SDL_Texture *MainWindow::createTexture(SDL_Renderer *renderer)
{

    SDL_Texture* texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_ABGR32,SDL_TEXTUREACCESS_TARGET,50,50);

    if(!texture) {

        qDebug() << "SDL_CreateTexture fail";
        return nullptr;
    }

    int res = SDL_SetRenderTarget(renderer,texture);

    if(res) {
        SDL_DestroyTexture(texture);
        return nullptr;
    }



//    res = SDL_SetRenderDrawColor(renderer,255,255,255,255);

//    SDL_RenderClear(renderer);

    SDL_SetRenderDrawColor(renderer,102,217,122,255);

    if(res) {

        qDebug() << "SDL_SetRenderDrawColor fail";
        SDL_DestroyTexture(texture);
        return nullptr;
    }


    SDL_RenderDrawLine(renderer,0,0,50,50);
    SDL_RenderDrawLine(renderer,50,0,50,50);
    SDL_RenderDrawLine(renderer,0,0,50,0);

    //重新设置绘制目标为window
//    SDL_SetRenderTarget(renderer,nullptr);
    return texture;
}




void MainWindow::mouseClick(SDL_Renderer *render, SDL_Texture *texture, SDL_Event &event)
{

   SDL_MouseButtonEvent buttonEvent =  event.button;

   qDebug() << "x:"  << buttonEvent.x << "y:" << buttonEvent.y;

//   SDL_RenderClear(render);


   int w = 0,h = 0;

   SDL_QueryTexture(texture,nullptr,nullptr,&w,&h);

   SDL_Rect rect = {buttonEvent.x - (w >> 1),buttonEvent.y - (h >> 1),w,h};

   SDL_RenderCopy(render,texture,nullptr,&rect);

   SDL_RenderPresent(render);
}
















